using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace MicroMsg.Storage
{
	public class SnsMediaStorage : IStorageLogin, IStorageFlush
	{
		private const int dir_num = 100;
		private const string TAG = "SnsMediaStorage";
		private static SnsHashDir hasher;
		private static string downLoadPath;
		private static string upLoadPath;
		private static string upLoadPathMapXml;
		private static int modifyNum;
		private static Random seed;
		private static SnsMediaStorage instance;
		private static SnsMediaInfo _mapUrl;
		static SnsMediaStorage()
		{
			SnsMediaStorage.hasher = new SnsHashDir();
			SnsMediaStorage.modifyNum = 0;
			SnsMediaStorage.seed = new Random();
			SnsMediaStorage.instance = new SnsMediaStorage();
			StorageBasic.registerLoginNotify(SnsMediaStorage.instance);
			StorageBasic.registerFlushNotify(SnsMediaStorage.instance);
			SnsMediaStorage.instance.onLogin(null);
		}
		public static SnsMediaInfo getMediaInfo()
		{
			if (SnsMediaStorage._mapUrl != null)
			{
				return SnsMediaStorage._mapUrl;
			}
			SnsMediaStorage._mapUrl = StorageXml.loadFromFile<SnsMediaInfo>(SnsMediaStorage.upLoadPathMapXml);
			if (SnsMediaStorage._mapUrl == null || SnsMediaStorage._mapUrl.mapPath == null)
			{
				SnsMediaStorage._mapUrl = new SnsMediaInfo();
			}
			return SnsMediaStorage._mapUrl;
		}
		public static void setMediaInfo(long fileSizeTotla, int fileNum)
		{
			if (fileNum < 0)
			{
				fileNum = 0;
			}
			SnsMediaInfo mediaInfo = SnsMediaStorage.getMediaInfo();
			mediaInfo.fileSizeTotal = fileSizeTotla;
			mediaInfo.fileNumber = fileNum;
			SnsMediaStorage.modifyNum++;
		}
		private static void tracksAddFile(int fileSize)
		{
			SnsMediaInfo mediaInfo = SnsMediaStorage.getMediaInfo();
			mediaInfo.fileSizeTotal += (long)fileSize;
			mediaInfo.fileNumber++;
			SnsMediaStorage.modifyNum++;
		}
		public void onLogin(string userName)
		{
			SnsMediaStorage.downLoadPath = StorageIO.getSnsPath() + "/down";
			SnsMediaStorage.upLoadPath = StorageIO.getSnsPath() + "/up";
			SnsMediaStorage.upLoadPathMapXml = StorageIO.getSnsPath() + "/urlmap.xml";
			SnsMediaStorage._mapUrl = null;
			SnsMediaStorage.modifyNum = 0;
			SnsMediaStorage.hasher.setParam(100, SnsMediaStorage.downLoadPath);
		}
		public void onLogout()
		{
			this.onFlush();
		}
		public void onFlush()
		{
			if (SnsMediaStorage.modifyNum > 0)
			{
				SnsMediaStorage.modifyNum = 0;
				StorageXml.saveToFile<SnsMediaInfo>(SnsMediaStorage._mapUrl, SnsMediaStorage.upLoadPathMapXml);
			}
		}
		public static bool save(string string_0, byte[] data_buffer)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return false;
			}
			SnsMediaStorage.tracksAddFile(data_buffer.Length);
			if (!SnsMediaStorage.isHttp(string_0))
			{
				return StorageIO.writeToFile(string_0, data_buffer, true);
			}
			if (SnsMediaStorage.getMediaInfo().mapPath.ContainsKey(string_0))
			{
				return StorageIO.writeToFile(SnsMediaStorage.getMediaInfo().mapPath.get_Item(string_0), data_buffer, false);
			}
			return SnsMediaStorage.hasher.write(MD5Core.GetHashString(string_0), new byte[][]
			{
				data_buffer
			});
		}
		public static bool isHttp(string string_0)
		{
			return !string.IsNullOrEmpty(string_0) && string_0.Substring(0, 4).ToLower() == "http";
		}
		public static byte[] load(string string_0)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return null;
			}
			if (!SnsMediaStorage.isHttp(string_0))
			{
				return StorageIO.readFromFile(string_0);
			}
			if (SnsMediaStorage.getMediaInfo().mapPath.ContainsKey(string_0))
			{
				return StorageIO.readFromFile(SnsMediaStorage.getMediaInfo().mapPath.get_Item(string_0));
			}
			return SnsMediaStorage.hasher.read(MD5Core.GetHashString(string_0));
		}
		public static bool exists(string string_0)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return false;
			}
			if (!SnsMediaStorage.isHttp(string_0))
			{
				return StorageIO.fileExists(string_0);
			}
			if (SnsMediaStorage.getMediaInfo().mapPath.ContainsKey(string_0))
			{
				return StorageIO.fileExists(SnsMediaStorage.getMediaInfo().mapPath.get_Item(string_0));
			}
			return SnsMediaStorage.hasher.exists(string_0);
		}
		public static bool addUrlMap(string string_0, string loaclPath)
		{
			if (string.IsNullOrEmpty(string_0) || string.IsNullOrEmpty(loaclPath))
			{
				return false;
			}
			if (SnsMediaStorage.getMediaInfo().mapPath.ContainsKey(string_0))
			{
				Log.smethod_1("SnsMediaStorage", "duplicate url map add!");
				return false;
			}
			SnsMediaStorage.getMediaInfo().mapPath.set_Item(string_0, loaclPath);
			SnsMediaStorage.modifyNum++;
			if (Debugger.get_IsAttached())
			{
				SnsMediaStorage.instance.onFlush();
			}
			return true;
		}
		public static void deleteUrlMap(string string_0)
		{
			if (string.IsNullOrEmpty(string_0) || string.IsNullOrEmpty(string_0))
			{
				return;
			}
			if (SnsMediaStorage.getMediaInfo().mapPath.ContainsKey(string_0))
			{
				Log.smethod_1("SnsMediaStorage", "deleteUrlMap error!");
				return;
			}
			SnsMediaStorage.getMediaInfo().mapPath.Remove(string_0);
			SnsMediaStorage.modifyNum++;
		}
		private static void ClearUrlMap()
		{
			SnsMediaInfo mediaInfo = SnsMediaStorage.getMediaInfo();
			if (mediaInfo.mapPath.get_Count() <= 0)
			{
				return;
			}
			mediaInfo.mapPath.Clear();
			SnsMediaStorage.modifyNum++;
		}
		public static void verifyUrlMap()
		{
			SnsMediaInfo mediaInfo = SnsMediaStorage.getMediaInfo();
			List<FileDetail> list = StorageIO.filesDetail(SnsMediaStorage.upLoadPath);
			if (list == null || list.get_Count() <= 0)
			{
				SnsMediaStorage.ClearUrlMap();
				return;
			}
			if (Enumerable.Count<FileDetail>(list) == Enumerable.Count<KeyValuePair<string, string>>(mediaInfo.mapPath))
			{
				return;
			}
			List<string> list2 = Enumerable.ToList<string>(Enumerable.Select<FileDetail, string>(list, (FileDetail fileDetail_0) => fileDetail_0.strFileName));
			List<string> list3 = new List<string>();
			using (Dictionary<string, string>.Enumerator enumerator = mediaInfo.mapPath.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<string, string> current = enumerator.get_Current();
					if (!list2.Contains(current.get_Value()))
					{
						list3.Add(current.get_Value());
					}
				}
			}
			if (list3.get_Count() <= 0)
			{
				return;
			}
			using (List<string>.Enumerator enumerator2 = list3.GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					string current2 = enumerator2.get_Current();
					mediaInfo.mapPath.Remove(current2);
				}
			}
			SnsMediaStorage.modifyNum++;
		}
		public static string getRandomPath()
		{
			return string.Concat(new object[]
			{
				SnsMediaStorage.upLoadPath,
				"/up_",
				Util.getNowMilliseconds(),
				"_",
				SnsMediaStorage.seed.Next(),
				".jpg"
			});
		}
		public static bool isUploadPath(string path)
		{
			return path == SnsMediaStorage.upLoadPath;
		}
		public static void ClearAll()
		{
			StorageIO.emptyDir(SnsMediaStorage.downLoadPath);
			StorageIO.emptyDir(SnsMediaStorage.upLoadPath);
			SnsMediaStorage.ClearUrlMap();
			SnsMediaStorage.setMediaInfo(0L, 0);
		}
		public static void test()
		{
			DebugEx.getTimeSpan();
			StorageIO.filesNumber(SnsMediaStorage.downLoadPath);
			List<FileDetail> list = StorageIO.filesDetail(SnsMediaStorage.downLoadPath);
			if (list == null)
			{
				return;
			}
			int num = 0;
			using (List<FileDetail>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					FileDetail arg_33_0 = enumerator.get_Current();
					num++;
				}
			}
		}
	}
}
